package py.com.fms.session;

import java.io.IOException;
import java.io.Reader;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Startup;
import org.jboss.seam.log.Log;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import py.com.fms.exception.CommonException;

@Name("serviceSqlMap")
@Scope(ScopeType.SESSION)
@Startup
public class ServiceSqlMap {
	 @Logger
	  private Log log;
	  private SqlMapClient sqlMapClient = null;
	  
	  public ServiceSqlMap() {
	  }
	  
	  public SqlMapClient getConfig() throws CommonException {
	    try {
	      if (this.sqlMapClient == null) {
	        String resource = "py/com/fms/persistence/sqlmap/SqlMapConfig.xml";
	        Reader reader = Resources.getResourceAsReader(resource);
	        this.sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
	      }
	      return this.sqlMapClient;
	    } catch (IOException e) {
	      this.log.error("Error en ServiceSqlMap : {0}", e.getMessage());
	      throw new CommonException(e);
	    }
	  }
	  

}
